﻿using Microsoft.EntityFrameworkCore;
using S.PregnancyTest.Domain;

namespace S.PregnancyTest.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {

        private readonly MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }

        #region 同步

        /// <summary>
        /// 同步添加方法
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Add(T t)
        {
            db.Set<T>().Add(t);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 同步删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var list = db.Set<T>().Find(id);
            db.Set<T>().Remove(list);
            return db.SaveChanges() > 0;
        }
        
        /// <summary>
        /// 同步查询方法
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        
        /// <summary>
        /// 同步修改方法
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Update(T t)
        {
            db.Set<T>().Update(t);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 同步批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool AddAll(List<T> t)
        {
            db.Set<T>().AddRange(t);
            return db.SaveChanges() > 0;
        }

        #endregion



        #region 异步

        /// <summary>
        /// 异步添加方法
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync() > 0;
        }

        /// <summary>
        /// 异步删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var list = await db.Set<T>().FindAsync(id);
            db.Set<T>().Remove(list);
            return await db.SaveChangesAsync() > 0;
        }

        /// <summary>
        /// 异步查询方法
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();
        }

        /// <summary>
        /// 异步修改方法
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync() > 0;
        }

        /// <summary>
        /// 异步批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> AddAllAsync(List<T> t)
        {
            await db.Set<T>().AddRangeAsync(t);
            return await db.SaveChangesAsync() > 0;
        }


        #endregion

    }
}